package com.xqls.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xqls.bean.SamplingRecordBean;
import com.xqls.biz.AdministrationBiz;
import com.xqls.biz.SamplingRecordBiz;
import com.xqls.dal.model.SamplingRecord;
import com.xqls.service.SamplingRecordService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DownloadAct;
import com.xqls.util.VelocityUtil;
@Service
public class SamplingRecordBizImpl implements SamplingRecordBiz{
	private static Logger logger = Logger.getLogger(SamplingRecordBiz.class);
	
	@Autowired private SamplingRecordService recordService;
	@Autowired private AdministrationBiz administrationBiz; 
	
	@Override
	public CommonResult insertSamplingRecordList(List<SamplingRecordBean> recordBeans) {
		CommonResult commonResult=new CommonResult();
		boolean success=false;
		try {
			for (SamplingRecordBean samplingRecordBean : recordBeans) {
				SamplingRecordBean recordBean=recordService.queryByRecordNo(samplingRecordBean.getRecordNo());
				if(recordBean==null){
					success=recordService.insertSamplingRecord(samplingRecordBean);
				}else{
					success=recordService.updateSamplingRecord(samplingRecordBean);
				}
			}
		} catch (Exception e) {
			logger.error("批量保存采样记录异常",e);
		}
		commonResult.setIsSuccess(success);
		return commonResult;
	}
	
	@Override
	public CommonResult queryRecordBySampleType(CommonPage commonPage, String sampleType, HttpServletRequest request, HttpServletResponse response) {
		CommonResult commonResult=new CommonResult();
		String tablename=null;
		try {
			if(sampleType.equals("discharge")){
				tablename="discharge_sampling_record";
			}else if(sampleType.equals("rainwater")){
				tablename = "rainwater_sampling_record";
			}else if(sampleType.equals("stormwater")){
				tablename = "public_sampling_record";
			}else if(sampleType.equals("wastegas")){
				tablename = "wastegas_sampling_record";
			}
			
			List<String> columns_name = new ArrayList<String>();
			List<Object> columns = new ArrayList<Object>();
			List<String> displayfiled = administrationBiz.conversion(tablename);
			columns.add("viewId");
			for (String display : displayfiled) {
				if(!"record_no".equals(display)){
					columns.add(display);
				}
			}
			columns_name.add("序号");
			List<String> fieldalias = administrationBiz.fieldalias(tablename);
			for (String fieldalia : fieldalias) {
				if (!"编号".equals(fieldalia)) {
					columns_name.add(fieldalia);
				}
			}
//			commonPage.setPageData(recordService.selectByMap(commonPage.pageToMap(), sampleType));
			List<Map<String, Object>> datas = recordService.selectByMap(commonPage.pageToMap(),sampleType);
			int curr=commonPage.getCurr();
			for (int i = 0;i<datas.size();i++){
				datas.get(i).put("viewId",i+1+((curr-1)*15));
			}
			commonPage.setPageData(datas);
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("recordList",commonPage.getPageData());
			showMap.put("columns",  CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
					columns.toArray(), null, null));
			commonPage.setHtml(VelocityUtil.getHtmlByMap("Sampling/samplingListData.vm",showMap,request,response,request.getSession().getServletContext()));
			commonResult.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("通过采样类型查询数据异常",e);
		}
		return commonResult;
	}
	
	public SamplingRecord queryRecordByDeviceNo(String deviceNo,String sampleType){
		return recordService.queryRecordByDeviceNo(deviceNo, sampleType);
	}
	
	@Override
	public void getListByMap(HttpServletRequest request,HttpServletResponse response,Map<String, Object> map) {
		try {
			List<Map<String,Object>> recordList = (List<Map<String,Object>>) this.recordService.getListByMap(map);
			if(recordList.isEmpty())
			{
				
			}
			else
			{
				String sheetName=null;
				String sampleType=(String) map.get("sampleType");
				
				if(sampleType.equals("discharge")){
					sheetName = "排放口采样记录";
				}else if(sampleType.equals("rainwater")){
					sheetName = "企业雨水排放口采样记录";
				}else if(sampleType.equals("stormwater")){
					sheetName = "公共雨水排放口采样记录";
				}else if(sampleType.equals("wastegas")){
					sheetName ="废气排放口采样记录";
				}
				
				String columnNames[]={"序号","排放口","瓶号","记录时间","留样量"};
				
				Set<String> keySet = recordList.get(0).keySet();
				String []keys=keySet.toArray(new String[keySet.size()]);
				
				DownloadAct.excelDownload(request, response, recordList, columnNames, keys, sheetName);
			}
		} catch (Exception e) {
			logger.error("导出采样统计数据异常",e);
		}
	}
}
